﻿using DaVikingCode.AssetPacker;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class TriagleDraw : MeshDrawBase
{
    public List<Vector3> verts = new List<Vector3>();
    AssetPacker assetPacker;
    MeshRenderer render;

    void Start()
    {
        this.assetPacker = new AssetPacker { useCache = false, cacheName = "p", cacheVersion = 2 };
        render = this.GetComponent<MeshRenderer>();

        //test
        for (int i = 1; i < 10; ++i)
        {
            string file = "/Sprites/Planes/Plane000" + i + ".png";
            StartCoroutine(CopyFileFromSAPathToPDPath(i, file, null, loaded2));
        }
    }

    protected override void DrawMesh()
    {
        
    }

    public IEnumerator CopyFileFromSAPathToPDPath(int index, string InSAName, string InPDPName = null, Action<int> InOnCopyFinishedAction = null)
    {
        UnityEngine.Assertions.Assert.IsFalse(string.IsNullOrEmpty(InSAName));
        if (string.IsNullOrEmpty(InPDPName)) InPDPName = InSAName;
        string streamingAssetPath = GetStreamingAssetsPath() + InSAName;
        string persistentDataPath = Application.persistentDataPath + InPDPName;

        var dirPath = persistentDataPath.Substring(0, persistentDataPath.LastIndexOf("/"));
        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }

        if (!File.Exists(persistentDataPath))
        {
#if UNITY_ANDROID
            WWW www = new WWW(streamingAssetPath);
            yield return www;

            if (www.isDone && string.IsNullOrEmpty(www.error))
            {
                File.WriteAllBytes(persistentDataPath, www.bytes);
                InOnCopyFinishedAction.Invoke(index);
            }
            else
                Debug.LogError("[CopyFileFromSAPathToPDPath] error:" + www.error);
#else
        File.Copy(streamingAssetPath, persistentDataPath, true);
#endif
        }
        else
        {
            InOnCopyFinishedAction.Invoke(index);
        }

        yield return null;
    }

    public string GetStreamingAssetsPath()
    {
#if UNITY_ANDROID
        return Application.streamingAssetsPath; //android比较特殊
#else
        return "file://" + Application.streamingAssetsPath; //pc和iOS可用
#endif
    }


    #region test
    private void loaded2(int index)
    {
        if (index == 9)
        {
            string[] files = Directory.GetFiles(Application.persistentDataPath + "/Sprites/Planes", "*.png");

            assetPacker.OnProcessCompleted += LaunchAnimations2;
            assetPacker.AddTexturesToPack(files);
            assetPacker.Process("Plane", 1024);
        }
    }

    void LaunchAnimations2(bool isSuccess, string msg)
    {
        if (isSuccess)
            LoadAnimation2();
    }

    void LoadAnimation2()
    {
        mesh = new Mesh();
        mesh.name = "triangle";

        mesh.vertices = verts.ToArray();

        tris = new int[6];
        tris[0] = 0;
        tris[1] = 1;
        tris[2] = 2;
        tris[3] = 0;
        tris[4] = 2;
        tris[5] = 3;

        mesh.triangles = tris;
        int index = UnityEngine.Random.Range(1, 9);
        SpriteInfo si = assetPacker.GetSprite("Plane000" + index);
        uvs = new Vector2[verts.Count];
        uvs = si.uvs;
        mesh.uv = uvs;

        var tex = assetPacker.FirstTexture;
        render.sharedMaterial.mainTexture = tex;

        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        mesh.RecalculateTangents();

        targetFilter.mesh = mesh;
    }
    #endregion
}
